<template>
  <div>
    <div class="navbar"><nav-bar1></nav-bar1></div>
    <ul class="topic_ul">
      <li @click="itemclick(item)"
          v-for="item in ranklist"
          :key="item.id"
          class="topic_item">
        <div class="topic_main">
          <img class="topic_media" :src="item.avatar"/>
          <div class="topic_info">
            <div class="topic_cont">
              <h3 class="topic_tit" v-html="item.name"></h3>
              <p
                :key="index"
                v-for="(song, index) in item.songList">
                {{index+1}}
                <span class="text_name">{{song.singername}}</span>
                {{song.songname}}
              </p>
            </div>
          </div>
        </div>
      </li>
    </ul>
   <keep-alive>
       <router-view/>
    </keep-alive>
    <tab-bar></tab-bar>
  </div>
</template>

<script>

import { GetRank } from 'api/rank'
import tabBar from '../base/tabBar'
import { mapMutations } from 'vuex'
import NavBar1 from 'base/NavBar1'

export default {
  name: 'recommend',
  created() {
    this._getRank()
  },
  data () {
    return {
      ranklist: [],
      songList: []
    }
  },
  components: {
    tabBar,
    NavBar1
  },
  methods: {
    // 获取榜单详情
    _getRank() {
      GetRank().then((res) => {
        if (res.code === 0) {
          this.ranklist = this._resetRankList(res.data.topList)
        }
      })
    },
    // 把排行榜的数据整理成我需要的格式
    _resetRankList(list) {
      var ret = []
      list.forEach((item) => {
        var singer = {
          name: item.topTitle,
          id: item.id,
          avatar: item.picUrl,
          listenCount: item.listenCount,
          songList: item.songList
        }
        ret.push(singer)
      })
      return ret
    },
    // 获取歌单详情
    itemclick (item) {
      this.$router.push({
        path: `/bangdanye/${item.listenCount}`
      })
      this.setSinger(item)
      window.scrollTo(0, 0)
    },
    ...mapMutations({
      setSinger: 'SET_SINGER'
    })
  }
}
</script>

<style scoped>
.topic_ul{
  padding: 10px;
  padding-bottom: 55px;
  background: #F4F4F4;
  padding-top: 55px;
}
.topic_item {
  margin-bottom: 10px;
  overflow: hidden;
}
.topic_main {
    display: -webkit-box;
    background: #fff;
}
.topic_media {
    position: relative;
    width: 100px;
    height: 100px;
    display: block;
}
.topic_info {
    position: relative;
    -webkit-box-flex: 1;
    display: -webkit-box;
    -webkit-box-align: center;
    -webkit-box-pack: center;
}
 .topic_cont {
    -webkit-box-flex: 1;
    margin: 0 10px 0 15px;
}
.topic_tit {
    font-size: .45rem;
    color: #000;
    font-weight: 400;
    margin-bottom: 5px;
}
.topic_cont p {
    font-size: .3rem;
    color: rgba(0,0,0,.5);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    line-height: .55rem;
}
 .text_name {
    color: #000;
    margin-left: 8px;
    margin-right: 5px;
}
</style>
